/**
 * Created by Administrator on 2017/4/20 0020.
 */
var CoverFlow={
    create:function (arr,cidx) {
        var coverFlow={};
        console.log(arr,cidx);
        coverFlow.currentIdx=cidx;

        var firstGap=210;
        var gap=135;
        var scaleGap=0.3;
        var trig=150;


        coverFlow.ui=new PIXI.DisplayObjectContainer();
        coverFlow.ui.interactive=true;
        coverFlow.ui.on('pointerdown',downHandler);
        coverFlow.ui.on('pointermove',moveHandler);
        coverFlow.ui.on('pointerup',upHandler);


        reFresh();
        function reFresh()
        {
            //中
            arr[coverFlow.currentIdx].ui.x=0;
            arr[coverFlow.currentIdx].ui.scale.x=1;
            arr[coverFlow.currentIdx].ui.scale.y=1;
            coverFlow.ui.addChildAt(arr[coverFlow.currentIdx].ui,0);
            //左
            for(var i=coverFlow.currentIdx-1;i>=0;i--)
            {
                coverFlow.ui.addChildAt(arr[i].ui,0);
                if(i==coverFlow.currentIdx-1)
                {
                    arr[i].ui.x =-firstGap;
                }
                else
                {
                    arr[i].ui.x =-firstGap-gap*(coverFlow.currentIdx-1-i);
                }
                arr[i].ui.scale.x= arr[i].ui.scale.y=1-scaleGap*(coverFlow.currentIdx-i);
            }
            //右
            for(var j=coverFlow.currentIdx+1;j<arr.length;j++)
            {
                coverFlow.ui.addChildAt(arr[j].ui,0);
                if(j==coverFlow.currentIdx+1)
                {
                    arr[j].ui.x =firstGap;
                }
                else
                {
                    arr[j].ui.x =firstGap+gap*(j-coverFlow.currentIdx-1);
                }

                arr[j].ui.scale.x= arr[j].ui.scale.y= 1-scaleGap*(j-coverFlow.currentIdx);
            }
        }

        function scrollReFresh(time)
        {
            tweening=true
            setTimeout(function () {
                tweening=false;

                document.dispatchEvent(new CustomEvent('Game_Update',{'detail':arr[coverFlow.currentIdx].idx}))

            },time*1000)
            //中
            TweenMax.to(arr[coverFlow.currentIdx].ui,time,{x:0});
            TweenMax.to(arr[coverFlow.currentIdx].ui.scale,time,{x:1,y:1});
            coverFlow.ui.addChildAt(arr[coverFlow.currentIdx].ui,0);
            //左
            for(var i=coverFlow.currentIdx-1;i>=0;i--)
            {
                coverFlow.ui.addChildAt(arr[i].ui,0);
                arr[i].ui.alpha=1;
                if(i==coverFlow.currentIdx-1)
                {
                    TweenMax.to(arr[i].ui,time,{x:-firstGap});
                }
                else
                {
                    TweenMax.to(arr[i].ui,time,{x:-firstGap-gap*(coverFlow.currentIdx-1-i)});
                    if(i==0)
                    {
                        arr[i].ui.alpha=0;
                    }
                }
                TweenMax.to(arr[i].ui.scale,time,{x:1-scaleGap*(coverFlow.currentIdx-i),y:1-scaleGap*(coverFlow.currentIdx-i)});
            }
            //右
            for(var j=coverFlow.currentIdx+1;j<arr.length;j++)
            {
                coverFlow.ui.addChildAt(arr[j].ui,0);
                arr[j].ui.alpha=1;
                if(j==coverFlow.currentIdx+1)
                {
                    TweenMax.to(arr[j].ui,time,{x:firstGap});
                }
                else
                {
                    TweenMax.to(arr[j].ui,time,{x:firstGap+gap*(j-coverFlow.currentIdx-1)});
                    if(j==arr.length-1)
                    {
                        arr[j].ui.alpha=0;
                    }
                }

                TweenMax.to(arr[j].ui.scale,time,{x:1-scaleGap*(j-coverFlow.currentIdx),y:1-scaleGap*(j-coverFlow.currentIdx)});
            }
        }

        function update(delta)
        {
            //中
            if(delta>0)
            {
                arr[coverFlow.currentIdx].ui.x=0+firstGap*delta/trig;
                arr[coverFlow.currentIdx].ui.scale.x=1-scaleGap*delta/trig;
                arr[coverFlow.currentIdx].ui.scale.y=1-scaleGap*delta/trig;
            }
            else
            {
                arr[coverFlow.currentIdx].ui.x=0+firstGap*delta/trig;
                arr[coverFlow.currentIdx].ui.scale.x=1+scaleGap*delta/trig;
                arr[coverFlow.currentIdx].ui.scale.y=1+scaleGap*delta/trig;
            }

            //左
            for(var i=coverFlow.currentIdx-1;i>=0;i--)
            {
                if(i==coverFlow.currentIdx-1)
                {
                    arr[i].ui.x =-firstGap+firstGap*delta/trig;
                }
                else
                {
                    arr[i].ui.x =-firstGap-gap*(coverFlow.currentIdx-1-i)+gap*delta/trig;
                }
                arr[i].ui.scale.x= arr[i].ui.scale.y=1-scaleGap*(coverFlow.currentIdx-i)+scaleGap*delta/trig;
            }
            //右
            for(var j=coverFlow.currentIdx+1;j<arr.length;j++)
            {
                if(j==coverFlow.currentIdx+1)
                {
                    arr[j].ui.x =firstGap+firstGap*delta/trig;
                }
                else
                {
                    arr[j].ui.x =firstGap+gap*(j-coverFlow.currentIdx-1)+gap*delta/trig;;
                }
                arr[j].ui.scale.x= arr[j].ui.scale.y= 1-scaleGap*(j-coverFlow.currentIdx)-scaleGap*delta/trig;
            }
        }

        function scrollTo() {

        }

        var bool_down;
        var mouseX;
        var downX;
        var detal=0;
        var tweening;
        function downHandler(e)
        {
            bool_down=true;
            mouseX=e.data.global.x;
            detal=0;
        }
        function moveHandler(e)
        {
            if(bool_down&&!tweening)
            {
                detal=1*(e.data.global.x-mouseX);
                update(detal);
                if(detal<-trig)
                {
                    // bool_down=false;
                    // detal=0;
                    mouseX=e.data.global.x;
                    arr.push(arr.shift());
                    scrollReFresh(0);
                }
                else if(detal>trig)
                {
                    // bool_down=false;
                    // detal=0;
                    mouseX=e.data.global.x;
                    arr.unshift(arr.pop());
                    scrollReFresh(0);
                }
            }
        }
        function upHandler(e)
        {
            bool_down=false;
            // var time=Math.abs(detal/150);
            // if(time>0.3)
            // {
            //     time=0.3;
            // }
            // if(time<=0.01)
            // {
            //     time=0.01;
            // }
            if(detal<-trig/3)
            {
                arr.push(arr.shift());
                scrollReFresh(0.3);
            }
            else if(detal>trig/3)
            {
                arr.unshift(arr.pop());
                scrollReFresh(0.3);
            }
            else
            {
                scrollReFresh(0.3);
            }
        }
        return coverFlow;
    }
}